﻿// Please see documentation at https://learn.microsoft.com/aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.

// Write your JavaScript code.

//主页事件
window.addEventListener("load", function () {
    // 菜单项点击事件
    document.querySelectorAll('.menu-item').forEach(item => {
        item.addEventListener('click', function () {
            // 如果点击的是有子菜单的项
            if (this.nextElementSibling && this.nextElementSibling.classList.contains('submenu')) {
                this.classList.toggle('expanded');
            } else {
                // 普通菜单项点击
                document.querySelectorAll('.menu-item').forEach(i => {
                    i.classList.remove('active');
                });
                this.classList.add('active');
            }
        });
    });

    // 子菜单项点击事件
    document.querySelectorAll('.submenu-item').forEach(item => {
        item.addEventListener('click', function () {
            // 子菜单点击，也要去除第一级所有菜单active状态
            document.querySelectorAll('.menu-item').forEach(i => {
                i.classList.remove('active');
            });
            // 去掉所有子菜单active状态，并添加点击子菜单的active状态
            document.querySelectorAll('.submenu-item').forEach(i => {
                i.classList.remove('active');
            });
            this.classList.add('active');
        });
    });

    // 退出登录确认事件
    document.querySelector("#btnConfirmLogout").addEventListener("click", function () {
        window.location.href = "/account/logout";
    });

    // 提交修改密码
    document.querySelector("#btnConfirmChangePWD").addEventListener("click", function () {
        var oldPassword = document.querySelector("#changePasswordOld").value;
        var newPassword = document.querySelector("#changePasswordNew").value;
        var confirmPassword = document.querySelector("#changePasswordConfirm").value;
        if (oldPassword.length == 0 || newPassword.length == 0 || confirmPassword.length == 0) {
            alert("修改信息不能为空！");
            return;
        }
        else if (newPassword.length < 6 || confirmPassword.length < 6 || newPassword.length > 50 || confirmPassword.length > 50) {
            alert("用户密码长度为6至50个字符！");
            return;
        }
        var formData = new FormData();
        formData.append("oldPassword", oldPassword);
        formData.append("newPassword", newPassword);
        formData.append("confirmPassword", confirmPassword);
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "/user/changepwd", true);
        xhr.send(formData);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    var result = JSON.parse(xhr.responseText);
                    if (result.success) {
                        //手动调用模态框隐藏
                        var cpModel = bootstrap.Modal.getInstance(document.querySelector("#changePasswordModal"));
                        if (cpModel) cpModel.hide();
                        document.querySelector("#changePasswordOld").value = "";
                        document.querySelector("#changePasswordNew").value = "";
                        document.querySelector("#changePasswordConfirm").value = "";
                        alert("修改成功！");
                    }
                    else {
                        alert(result.message ?? "修改失败！");
                    }
                }
                else {
                    alert("修改失败，请稍后重试！");
                }
            }
        };
    });
});

//返回上一页
function backToPrevPage() {
    window.history.back();
}